/**
 * 将毫秒，转换成时间字符串。例如说，xx 分钟
 *
 * @param ms 毫秒
 * @returns {string} 字符串
 */
export function getDate(ms) {
  const day = Math.floor(ms / (24 * 60 * 60 * 1000));
  const hour =  Math.floor((ms / (60 * 60 * 1000) - day * 24));
  const minute =  Math.floor(((ms / (60 * 1000)) - day * 24 * 60 - hour * 60));
  const second =  Math.floor((ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60));
  if (day > 0) {
    return day + "天" + hour + "小时" + minute + "分钟";
  }
  if (hour > 0) {
    return hour + "小时" + minute + "分钟";
  }
  if (minute > 0) {
    return minute + "分钟";
  }
  if (second > 0) {
    return second + "秒";
  } else {
    return 0 + "秒";
  }
}

export function beginOfDay(date) {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

export function endOfDay(date) {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999);
}

export function betweenDay(date1, date2) {
  date1 = convertDate(date1);
  date2 = convertDate(date2);
  // 计算差值
  return Math.floor((date2.getTime() - date1.getTime()) / (24 * 3600 * 1000));
}

export function formatDate(date, fmt) {
  date = convertDate(date);
  const o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "H+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(), //秒
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度
    "S": date.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) { // 年份
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (const k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}

export function addTime(date, time) {
  date = convertDate(date);
  return new Date(date.getTime() + time);
}

export function convertDate(date) {
  if (typeof date === 'string') {
    return new Date(date);
  }
  return date;
}
/**
 *
 * @param {*} date  格式化后的时间  yyyy-mm-dd
 * @param {*} monthNum 几个月
 * @returns
 */
export function GetPreMonthDay(date, monthNum) {
  var dateArr = date.split('-');
  var year = dateArr[0]; //获取当前日期的年份
  var month = dateArr[1]; //获取当前日期的月份
  var day = dateArr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) - monthNum;
  if (month2 <= 0) {
      var absM = Math.abs(month2);
      year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12);
      month2 = 12 - (absM % 12);
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
      day2 = days2;
  }
  if (month2 < 10) {
      month2 = '0' + month2;
  }
  var t2 = year2 + '-' + month2 + '-' + day2;
  return t2
}

/**
 * 获取传入时间的月初时间和月末时间
 *
 * @param {*} val
 * @returns
 */
 export function getBeginAndEndTime(val) {
  let date = new Date(val);
  let new_year = date.getFullYear(); // 取当前的年份
  let month = date.getMonth();
  let new_month = month + 1; // 取当前的月份
  let mon = "";
  let day = "";
  if (month > 12) {
    new_month -= 12; // 月份减
    new_year++; // 年份增
  }
  let firstDay = new Date(new_year, new_month, 1); // 取当年当月中的第一天
  let lastDay = new Date(
    firstDay.getTime() - 1000 * 60 * 60 * 24
  ).getDate(); // 获取当月最后一天日期
  if (firstDay.getMonth() < 10) {
    mon = "0" + firstDay.getMonth();
  } else {
    mon = firstDay.getMonth();
  }
  if (lastDay < 10) {
    day = "0" + lastDay;
  } else {
    day = lastDay;
  }
  let startDate =
    firstDay.getFullYear() +
    "-" +
    mon +
    "-" +
    "0" +
    firstDay.getDate()+" 00:00:00";
  let endDate = firstDay.getFullYear() + "-" + mon + "-" + day+" 23:59:59";
  var createTime= [startDate , endDate];
  return createTime
}
